package data;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class FileManager {
	public void delete(File file) {
		if (file.isDirectory()) {
			for (File subFile : file.listFiles()) {
				delete(subFile);
			}
		}
		if (file.exists()) {
			if (!file.delete()) {
				System.out.println("Could not delete {}" + file);
			}
		}
	}

	public FileInputStream getInputStream(String name) throws FileNotFoundException {
		return new FileInputStream(name);
	}

	public FileOutputStream getOutputStream(String name) throws FileNotFoundException {
		createFile(name, null);
		return new FileOutputStream(name);
	}

	public void downloadFile(String url, String filename, long fileSize) throws InterruptedException {
		try {
			Download download = new Download(url, filename);

			while (download.getStatus() != Download.COMPLETE || download.getStatus() != Download.ERROR
					|| download.getStatus() != Download.CANCELLED) {
				Thread.sleep(100);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void createDirectory(String path) {
		try {
			if (new File(path).exists()) {
				return;
			}

			if (path.split("/").length < 1 && path.split("\\").length < 1) {
				new File(path).mkdir();
			} else {
				new File(path).mkdirs();
			}
		} catch (Throwable t) {
			System.out.print(t.getMessage());
		}
	}

	public void createFile(String filename, ArrayList lines) {
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(filename, false));
			if (lines != null) {
				for (int i = 0; i < lines.size(); i++) {
					out.write((String) lines.get(i));
					out.newLine();
				}
			}
			out.close();
		} catch (Throwable t) {
			System.out.print(t.getMessage());
		}
	}

	public void appendFile(String filename, boolean skipLines, ArrayList lines) {
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(filename, true));
			out.newLine();
			for (int i = 0; i < lines.size(); i++) {
				out.write((String) lines.get(i));
				if (skipLines) {
					out.newLine();
				}
			}
			out.close();
		} catch (Throwable t) {
			System.out.print(t.getMessage());
		}
	}

	public String[] getFiles(String directory, String ext) {
		final String extension = ext;
		File dir = new File(directory);
		return dir.list(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(extension);
			}
		});
	}

	public String[] getStringArray(String fileName) {
		ArrayList<String> list = new ArrayList<String>();

		try {
			BufferedReader itemData = new BufferedReader(new FileReader(fileName));

			String line = null;
			while ((line = itemData.readLine()) != null) {
				list.add(line);
			}
		} catch (IOException e) {
			System.out.println("Failed to load from file");
			e.printStackTrace();
		}

		return list.toArray(new String[list.size()]);

	}

	public void extractZip(String zipFile, String destination) throws IOException {
		ZipFile jar = new ZipFile(zipFile);
		Enumeration enume = jar.entries();
		while (enume.hasMoreElements()) {
			ZipEntry file = (ZipEntry) enume.nextElement();
			File f = new java.io.File(destination + File.separator + file.getName());
			if (file.isDirectory()) { // if its a directory, create it
				f.mkdir();
				continue;
			}
			InputStream is = jar.getInputStream(file);
			FileOutputStream fos = new FileOutputStream(f);
			while (is.available() > 0) {
				fos.write(is.read());
			}
			fos.close();
			is.close();
		}
	}
}